import { UploadRequest } from "@/utils/request.js";


const upLoadFile = (file) => {
  const FILE_MAX_SIZE = 1024 * 1024 * 30 // 30M
  const FILE_TYPE = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp', 'application/pdf']

  if (!file) {
    throw new Error('请选择文件')
  }

  if (file.size > FILE_MAX_SIZE) {
    throw new Error('文件大小超过 30MB')
  }
  if (file.size === 0) {
    throw new Error('文件大小为0')
  }

  if (!FILE_TYPE.includes(file.type)) {
    throw new Error('不支持的文件类型')
  }

  const formData = new FormData();
  formData.append('file', file);
  return UploadRequest('fileManagement/uploadFile', formData, { method: 'post' })
}

export { upLoadFile };